angular.module("myApp")
.controller("SearchController",["$scope","$http","$state","globalData",function($scope,$http,$state,globalData){
	globalData.getGoods_filter().success(function(res){
		var allgood=[],
			goods=[];
		angular.forEach(res,function(value,keys){
				angular.forEach(value,function(val,key){
					var va=val.itemList;
					angular.forEach(va,function(v,k){
						allgood.push(v);
					});
				});
			});

		$scope.allgood=[];	
		$scope.keyWord ="";
		var conList =[],
			hisArr =[],
			flag =false;		
		//关键字列表函数
		var keylog = [];
		$scope.keyList =function(keyWord){
			//清空原来列表
			keylog=[];
			$scope.allgood=[];
			angular.forEach(res,function(value,key){
				if(key == keyWord){
					for(var n=0;n<value.length;n++){
						keylog.push(value[n]);
					}
					//更新列表内容
					$scope.keylog = keylog;
					keyWord="";
				}else if(keyWord !=""){
					$scope.keylog = [];
					$scope.allgood=allgood;
				}
			});
		};
		
		$scope.keynull=function(){
			$scope.keyWord="";
			$scope.keylog = [];
		}
		//点击提示列表
		$scope.keytext=function(e){
			$scope.text=e.target.innerHTML;	
			//更新历史记录
			hisArr.push({hisWord:$scope.keyWord,histext:e.target.innerHTML});
			$scope.hisArr =hisArr;
			//加入列表
			mainList($scope.text);
			//更新列表内容
			$scope.conLists = conList;
			//显示历史记录
			$scope.hisshow=true;	
		}
	
		//筛选后的列表函数
		function mainList(keytext){
			conList=[];
			angular.forEach(res,function(value,key){
				if(key ==$scope.keyWord){
					angular.forEach(value,function(val,keys){
						if(val.category.name ==keytext){
							conList=val.itemList;
						}
					},conList);
				}
			});
		};
		//是否显示历史记录
		$scope.hisshow=flag;
		$scope.ishis=ishistory;
		function ishistory(){
			hisArr =[];
			$scope.hisshow=flag;
			$scope.hisArr =hisArr;
		}
		//点击历史记录
		$scope.his=function(e){
			$scope.keyWord=e.target.getAttribute("data-keyword");
			$scope.text=e.target.innerHTML;
			mainList($scope.text);
			//更新列表内容
			$scope.conLists = conList;
		}
		
		//热门搜索
		$scope.hotArr =[{hotWord:"居家",hotText:"家具"},{hotWord:"餐厨",hotText:"餐具"},{hotWord:"配件",hotText:"鞋"},{hotWord:"服装",hotText:"外衣"},{hotWord:"服装",hotText:"衬衫"},{hotWord:"洗护",hotText:"美妆"},{hotWord:"饮食",hotText:"果干"},{hotWord:"杂货",hotText:"旅行"}];

		//价格排序
		$scope.updw='';
		$scope.ordkey='';
		var fog=true;
		$scope.updws=function(){
			if(fog){
				$scope.updw=false;
				$scope.ordkey='retailPrice';
				fog=false;
				$scope.up=true;
				$scope.down=false;
			}else{
				$scope.updw=true;
				$scope.ordkey='retailPrice';
				fog=true;
				$scope.up=false;
				$scope.down=true;
			}
		};
		$scope.index="0";
		//点击切换样式
		$scope.changecolor=function(e){
			$scope.index=e.target.getAttribute("data-index");
			if($scope.index==0 ||$scope.index==2){
				$scope.up=false;
				$scope.down=false;
				$scope.updw='';
				$scope.ordkey='';
			}
		}
		
	});
}])